#include "mainwidget.h"
#include "ui_mainwidget.h"


#include <QMessageBox>
#include <QDebug>

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    m_pObjCanMgr = new CanMgr();
    qRegisterMetaType<PVCI_CAN_OBJ>("PVCI_CAN_OBJ"); 
    
}

MainWidget::~MainWidget()
{
    delete ui;
    delete m_pObjCanMgr;
}


void MainWidget::on_BtnOpen_clicked()
{
    if(ui->BtnOpen->text() == "打开")
    {
        DWORD baudIdx = ui->comboBoxBaud->currentIndex();
        qDebug()<<"baudIdx:"<<baudIdx<<endl;
        
        if(m_pObjCanMgr->open(baudIdx))
        {
            ui->BtnOpen->setText("关闭");
            ui->comboBoxBaud->setEnabled(false);
            //启动线程
            if(!m_pObjCanMgr->isRunning())
            {
                m_pObjCanMgr->start();
            }
            connect(m_pObjCanMgr,SIGNAL(sendedInfoSignal(PVCI_CAN_OBJ)),this,SLOT(onSendCanData(PVCI_CAN_OBJ)));
            connect(m_pObjCanMgr,SIGNAL(getCanData(PVCI_CAN_OBJ,quint32)),this,SLOT(onRecvCanData(PVCI_CAN_OBJ,quint32)));
        }
    }
    else
    {
        //停止线程
        m_pObjCanMgr->quit();
        m_pObjCanMgr->close();
        ui->BtnOpen->setText("打开");
        ui->comboBoxBaud->setEnabled(true);
        disconnect(m_pObjCanMgr,SIGNAL(sendedInfoSignal(PVCI_CAN_OBJ)),this,SLOT(onSendCanData(PVCI_CAN_OBJ)));
        disconnect(m_pObjCanMgr,SIGNAL(getCanData(PVCI_CAN_OBJ,quint32)),this,SLOT(onRecvCanData(PVCI_CAN_OBJ,quint32)));
    }
}


void MainWidget::on_BtnSend_clicked()
{
    if(ui->BtnOpen->text() == "关闭")
    {
        VCI_CAN_OBJ sendObj;
        memset(&sendObj,0,sizeof(sendObj));
        
        QString IdStr = ui->lineEditId->text().simplified();
        
        if(IdStr.isEmpty())
        {
            QMessageBox::information(this,"提示","id不能为空");
            return;
        }
        
        UINT canID = IdStr.toUInt(nullptr,16);
        qDebug()<< "canID:"<<canID<<endl;
        sendObj.ID = canID;
        
        //发送类型
        sendObj.SendType = CAN_SEND_NORMAL;
        //数据类型
        sendObj.RemoteFlag = CAN_DATA_INFO;
        //是否扩展帧
        sendObj.ExternFlag = CAN_FRAM_EXTERN;
        
        QString DataStr = ui->lineEditData->text().simplified();
        //数据长度
        sendObj.DataLen = DataStr.remove(QRegExp("\\s")).size()/2;
        //数据内容
        QByteArray DataByte = QByteArray::fromHex(DataStr.toUtf8());
        memcpy(sendObj.Data,DataByte.data(),sendObj.DataLen);
        m_pObjCanMgr->send(sendObj);
    }
    else
    {
        QMessageBox::information(this,"提示","未打开设备！");
    }
}

void MainWidget::onRecvCanData(PVCI_CAN_OBJ objs,quint32 count)
{
    //qDebug()<< "count"<<count<<endl;
    
    for(quint32 i = 0;i < count;i++)
    {
        showCanInfo(false,objs+i);
    }
}


void MainWidget::onSendCanData(PVCI_CAN_OBJ obj)
{
    showCanInfo(true,obj);
}

void MainWidget::showCanInfo(bool isSend,PVCI_CAN_OBJ obj)
{
    QString StrPrefix;
    QString StrText;
    QString StrData;
    
    if(isSend)
    {
        StrPrefix.sprintf("Tx:");
    }
    else
    {
        StrPrefix.sprintf("Rx:");
    }
    
    StrText.sprintf("id:0x%08x len:%d data:0x",obj[0].ID,obj[0].DataLen);
    for(quint32 j = 0;j < obj[0].DataLen;j++)
    {
        QString StrTmp;
        StrTmp.sprintf("%02x ",obj[0].Data[j]);
        StrData.append(StrTmp);
    }
    
    StrText.append(StrData);
    StrPrefix.append(StrText);
    
    ui->textEditInfo->append(StrPrefix);
    qDebug()<<StrPrefix<<endl;
}


void MainWidget::on_pushButtonClear_clicked()
{
    ui->textEditInfo->clear();
}
